---
title: "Table A.13"
output: 
---

# Table A.13

# Who's to Blame? Postconflict Violence and Public Attitudes Towards Peace Agreements
# Wyer, Frank. 

#clear environment
```{r clear environment}
rm(list = ls())
```

# uncomment and set working directory to replication archive
# setwd("~/blame_replication")

# Uncomment to install packages if necessary
# install.packages("tidyverse")
# install.packages("estimatr")
# install.packages("texreg")


#load packages
```{r}
library(tidyverse)
library(estimatr)
library(texreg)

```

#read in data
```{r}
survey_clean <- read.csv("survey_clean.csv")
```

#predict support for peace using covariates in control group 
```{r predicted peace support}
#model peace outcome with covariates from control group respondents 
peace_support_modeled <- lm(outcomes_zscale ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), survey_clean %>% filter(treatment == "C"))

#predict values for full sample
survey_clean$psz_predicted <- predict(peace_support_modeled, newdata = survey_clean)
```

#clean and generate outcome variables
```{r outcome vars}
survey_clean <- survey_clean %>% 
mutate(
  govparty = ifelse(Q56 == 6 | Q56 == 7 | Q56 == 8 | Q56 == 2 | Q56 == 14, 1, 0), #respondent expresses affinity for one of 5 parties in governing coalition
  noparty = ifelse(Q56 == 19, 1, 0), #respondent expresses affinity for no political party
challengersupport = case_when( #respondents' level of affinity for challenger to incumbent president
  Q55 == 1 ~ 6,
  Q55 == 2 ~ 5,
  Q55 == 3 ~ 4,
  Q55 == 4 ~ 3,
  Q55 == 5 ~ 2,
  Q55 == 6 ~ 1,
  TRUE ~ NA_real_
  )) # changing scale order so that close (1) is the largest value and far (6) is the smallest
```

#scale challenger support variable relative to control group mean and SE
```{r scale outcomes}
survey_clean$challengersupport_scale <- (survey_clean$challengersupport - mean(survey_clean$challengersupport[survey_clean$treatment == "C"], na.rm = TRUE)) / sd(survey_clean$challengersupport[survey_clean$treatment == "C"], na.rm = TRUE)
```

#estimate treatment effects on each alternative outcome
```{r estimate treatment effects}
#support for governing parties: model with covariates
m_govparty <- lm_lin(govparty ~ treatment, covariates = ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), se_type = "HC2", data = survey_clean)

#support for governing parties: model with predicted peace support interaction
m_govparty_interact <- lm_robust(govparty ~ treatment*psz_predicted, se_type = "HC2", data = survey_clean)

#support for no political party: model with covariates
m_noparty <- lm_lin(formula = noparty ~ treatment, covariates = ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), se_type = "HC2", data = survey_clean)

#support for no political party: model with predicted peace support interaction
m_noparty_interact <- lm_robust(formula = noparty ~ treatment*psz_predicted, se_type = "HC2", data = survey_clean)

#support for challenger candidate: model with covariates
m_challenger <- lm_lin(formula = challengersupport_scale ~ treatment, covariates = ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + homratediff + factor(regionname), se_type = "HC2", data = survey_clean)

#support for challenger candidate: model with predicted peace support interaction
m_challenger_interact <- lm_robust(formula = challengersupport_scale ~ treatment*psz_predicted, se_type = "HC2", data = survey_clean)
```

#table for results
```{r results table}
texreg(list(m_govparty, m_govparty_interact, m_noparty, m_noparty_interact, m_challenger, m_challenger_interact), digits = 2, include.ci = FALSE, single.row = FALSE, include.fstatistic = FALSE, include.rmse = FALSE, include.rsquared = FALSE, include.adjrs = FALSE, include.nobs = TRUE, stars = numeric(0), float.pos = "H", caption.above	= TRUE, caption = "Treatment Effects on Political Outcomes", custom.coef.map = list("treatmentT1" = "Postconflict Violence Treatment", "treatmentT2A" = "Government Culpability Treatment", "treatmentT2B" = "Rebel Culpability Treatment", "psz_predicted" = "Predicted Peace Agreement Support", "treatmentT1:psz_predicted" = "Postconflict Violence Treatment x Predicted Peace Agreement Support", "treatmentT2A:psz_predicted" = "Government Culpability Treatment x Predicted Peace Agreement Support", "treatmentT2B:psz_predicted" = "Rebel Culpability Treatment x Predicted Peace Agreement Support"), custom.gof.rows = list("Controls" = c("\\checkmark", "", "\\checkmark", "", "\\checkmark", "")))
```

